# tasks/models.py
from django.db import models
from users.models import User


class Task(models.Model):
    # 枚举类型的选择
    TASK_TYPE_CHOICES = [
        ('folder', 'Folder'),  # 任务集
        ('task', 'Task'),  # 具体任务
    ]

    TIMER_MODE_CHOICES = [
        ('count_up', 'Count Up'),  # 向上计时
        ('count_down', 'Count Down')  # 向下计时
    ]

    # 任务字段
    user = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'tasks')  # 用户外键
    parent = models.ForeignKey('self', on_delete = models.CASCADE, null = True, blank = True, related_name = 'subtasks')  # 父任务外键
    path = models.CharField(max_length = 255, null = True, blank = True)  # 路径字符串
    position = models.IntegerField(default = 0)  # 同级显示顺序
    type = models.CharField(max_length = 20, choices = TASK_TYPE_CHOICES)  # 任务类型（任务集或任务）
    title = models.CharField(max_length = 100)  # 显示名称
    color = models.CharField(max_length = 7, default = '#4285F4')  # 颜色代码
    target_date = models.DateField(null = True, blank = True)  # 任务目标日期
    timer_mode = models.CharField(max_length = 20, choices = TIMER_MODE_CHOICES, null = True, blank = True)  # 计时方式
    duration = models.IntegerField(null = True, blank = True)  # 单次时长
    created_at = models.DateTimeField(auto_now_add = True)  # 创建时间
    updated_at = models.DateTimeField(auto_now = True)  # 修改时间

    class Meta:
        ordering = ['position']  # 同级任务按顺序排列

    def __str__(self):
        return self.title
